<?php
/**
 * 找好物页
 * Class FindWikiAction
 */

use Biz\model\ShopCategoryClassify;
use Biz\service\wiki\FindWIkiService;
use Biz\service\wiki\Wiki;
use Biz\service\wiki\WikiMiddle;
use Biz\tools\helper\Common;
use Doraemon\pockets\constInterface\statSign\AutoModule;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\Curl;
use Doraemon\tools\HhzClient;
use Doraemon\tools\HhzEnv;
use Doraemon\tools\Log;

class FindWikiAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public const TAB_TYPE = 'find_wiki_new';
    public const CONFIG_FILE = 'recommendation_api';
    public const CONFIG_SELECT = 'wiki';
    const IS_LOG_EXPOSE = 1;
    const PAGE_SIZE = 10;
    public const VALIDATOR_RULES = [
        'classify_id' => 'require|integer',
        'page' => 'integer',
        'act_from' => 'string',
    ];
    const IS_DEBUG = false;//debug 开关  上线关闭

    public function main()
    {
        try {
            $page = $this->params['page'] ?? 1;
            $act_from = $this->params['act_from'] ?? 'find_wiki';
            $definedCateId = $this->params['classify_id'] ?: 0;
            //圣诞活动
            if (in_array($definedCateId, [220118, 220119, 220120])) {//22年新年分类
                $wiki_ids = $this->getNewYearsWikis($definedCateId, $page, 20);
            } else {
                $mapClassify = FindWIkiService::FIND_WIKI_CLASSIFY;
                $classifyList = $mapClassify[$definedCateId] ?? $mapClassify[0];
                $cate = $classifyList['name'];
                $classifyNames = $classifyList['classify'] ? array_column($classifyList['classify'], 'name') : $mapClassify[0]['name'];
                $classifyIds = $classifyList['classify'] ? array_column($classifyList['classify'], 'id') : 0;

                $uid = $this->uid;
                $page = $page ?: 1;
                $url = Common::getUrl(self::TAB_TYPE, self::CONFIG_FILE, self::CONFIG_SELECT);
                $url .= '?uid=' . $uid . '&cate_name=' . urlencode($cate) . '&rec_length=' . self::PAGE_SIZE;
                //算法获取wiki_id 合集
                $mesRes = [];
                $result = Curl::Request('GET', $url);
                if ($result['code'] === 1) {
                    $info = $result['json']['data'];
                    $mesRes = $info['content_list'];
                }
                if (!$mesRes) {
                    if ($definedCateId == 0) {
                        $result = ShopCategoryClassify::selectInfos([
                            'status' => ShopCategoryClassify::STATUS_NORMAL,
                            'parent_id' => 0,
                            'ORDER' => ['sort' => 'DESC']
                        ]);
                        $classifyIds = array_column($result, 'id');
                        $randKey = array_rand($classifyIds);
                        $classifyId = $classifyIds ? $classifyIds[$randKey] : 0;
                    } else {
                        $randKey = array_rand($classifyIds);
                        $classifyId = $classifyIds ? $classifyIds[$randKey] : 0;
                    }
                    //兜底方案
                    $wiki_ids = FindWIkiService::getWikiIdsByClassifyId($classifyId, $page, self::PAGE_SIZE);
                } else {
                    $wiki_ids = array_column($mesRes, 'obj_id') ?? [];
                }
            }
            if (self::IS_DEBUG && $this->params['page'] <= 1 && $this->params['classify_id'] == 0) {
                if (HhzEnv::isGray()) {
                    $wiki_ids[0] = 195952;
                    $wiki_ids[1] = 192018;
                    $wiki_ids[2] = 139133;
                    $wiki_ids[3] = 182147;
                    $wiki_ids[4] = 196074;
                } else if (HhzEnv::isTest()) {
                    $wiki_ids = [7363, 6773, 6732, 7360, 6731, 7358, 6771, 7356, 6775, 6797];
//                    $wiki_ids[0] = 6799;
//                    $wiki_ids[1] = 6798;
//                    $wiki_ids[2] = 6801;
//                    $wiki_ids[3] = 1710;
//                    $wiki_ids[4] = 1695;
                }
                //$wiki_ids = [142893, 142892, 142884, 142871, 142862];//开发机debug数据
            }

            $wiki_ids = array_unique($wiki_ids);
            $rs = $this->formatMasonryLayouts($wiki_ids, $act_from, $definedCateId);

            $log = [
                'url' => $url,
                'mes' => $mesRes,
                'wiki_id' => $wiki_ids,
            ];
            Log::info('找好物请求日志', $log);
            $is_over = 0;
            if (!$rs || count($rs) < self::PAGE_SIZE) {
                $is_over = 1;
            }
            $res['is_over'] = $is_over;
            $res['current_page'] = $page;
            $res['list'] = $rs;
        } catch (UnexpectedValueException $e) {
            return $this->setSuccess(['msg' => $e->getMessage()], 0);
        } catch (Exception $e) {
            Log::error('获取发现好物列表失败-error:' . $e->getMessage(), $this->getRequest()->getPost());
            return $this->setSuccess(['msg' => '系统错误: ' . $e->getMessage()], 0);
        }
        return $this->setSuccess($res);
    }

    /**
     * 双列瀑布流
     */
    public function formatMasonryLayouts($wiki_ids, $act_from, $classifyId)
    {
        $list = [];
        $infos = Wiki::getWikiList($wiki_ids, 0, Wiki::FIELDS_GROUP_MASONRY_LIST);
        foreach ($infos as $wikiId => $row) {
            $data['type'] = BlockType::TYPE_WIKI;

            $middleLink = WikiMiddle::getMiddleLink($row);
            //wiki结构更改
            $data['wiki'] = $row;
            $data['wiki']['middle_link'] = $middleLink;

            $data['statSign'] = basicStatSign::genStatSign(AutoModule::MODULE_FIND_WIKI_WIKI_CARD, [
                basicStatSign::EXTEND_KEY_OBJ_ID => $wikiId,
                basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI
            ],
                [
                    'wiki_id' => $wikiId,
                    'tab_id' => $classifyId,
                    'tab_name' => FindWIkiService::FIND_WIKI_CLASSIFY[$classifyId]['name'] ?? '',
                    'uid' => $this->uid,
                    'act_from' => $act_from,
                ]);
            $list[] = $data;
        }
        return $list;
    }

    /**
     * 圣诞节商品清单（废弃）
     * @param $page
     * @param $perPage
     * @return array|int[]
     * @deprecated
     */
    private static function getChristmasWikis($page, $perPage)
    {
        $wikiIds = [183388, 88950, 87388, 182583, 86140, 87664, 87248, 91844, 187067, 187064, 93317, 89023, 87433, 189594, 92077, 88381, 30642, 186623, 187443, 93320, 90045, 89668, 92730, 190100,
            89356, 92505, 190601, 190039, 173850, 101818, 92829, 92764, 92569, 90327, 89325, 89314, 88948, 88079, 86113, 77750, 30527, 90193, 190102, 87478, 30538, 190245, 189878, 189748, 189738, 189124,
            187065, 186299, 186136, 185433, 117181, 93319, 93065, 92936, 92927, 92856, 92831, 92824, 92799, 92795, 92491, 92390, 92386, 92280, 91758, 90496, 90302, 90144, 89321, 89288, 88951, 88935, 88379,
            88359, 88286, 88043, 87790, 87510, 87352, 87344, 31381, 30608, 29665, 189309, 106104, 96406, 92285, 89405, 89307, 88117, 190614, 190420, 190375, 190374, 190162, 190161, 190097, 189919, 189879,
            189831, 189828, 189308, 189307, 189137, 187347, 187078, 187077, 187076, 186137, 184952, 184299, 159739, 151460, 141841, 103358, 99360, 98103, 95673, 95672, 95505, 93592, 93591, 93267,
            93224, 92953, 92941, 92870, 92860, 92814, 92797, 92773, 92763, 92726, 92493, 92490, 92489, 92447, 92433, 92126, 91958, 91839, 90622, 90589, 90538, 90509, 90506, 90438, 90370, 90012, 90006, 89666, 89317,
            89204, 89052, 89049, 89048, 89047, 89034, 89027, 88540, 88434, 88383, 88380, 88336, 88335, 88306, 88284, 88178, 88172, 88100, 88086, 88059, 87987, 87968, 87933, 87563, 87417, 87406, 87384, 87355, 87349,
            87303, 87234, 87184, 86433, 85867, 85866, 85815, 85814, 85813, 84261, 84122, 83077, 82793, 82312, 68768, 68701, 68568, 64793, 43771, 39369, 38303, 32191, 31626, 31562, 31528, 31469, 31415, 31356, 31355,
            31230, 31225, 31199, 30940, 30719, 30641, 30565, 30229, 29784, 24693, 20203, 19901, 18655, 18120, 14384, 13766, 13762, 13043, 12447, 8708, 8707, 8706, 8704, 7901];
        //偏移量，当前页-1乘以每页显示条数
        $start = ($page - 1) * $perPage;

        shuffle($wikiIds);
        return array_slice($wikiIds, $start, $perPage);
    }

    private function getNewYearsWikis($classifyId, $page, $perPage)
    {
        $arr1 = [
            197286, 190231, 162054, 190192, 181768, 164504, 185502, 190974, 192751, 197734, 198735, 197349, 197215, 181231, 190202, 194622, 190703, 198733, 196882, 195531, 173845, 195049, 187903, 184374, 186688, 199156, 193543, 193159, 185068, 190671, 195593, 191281, 195047, 193999, 192538, 197804, 194944, 191221, 185603, 181853, 197775, 197692, 197178, 195415, 199336, 198493, 198489, 198484, 197346, 186687, 197750, 196912, 190893, 183359, 182663, 198492, 197803, 197216, 187585, 192791, 185434, 163481, 199197, 198695, 198414, 197608, 193442, 191796, 200368, 199505, 199196, 198909, 198521, 197100, 184173, 172800, 199051, 200612, 200610, 200352, 199532, 199515, 199420, 199372, 199303, 199027, 198971, 198811, 198731, 198629, 197962, 197958, 197956, 197954, 197858, 197782, 197772, 197754, 197746, 197740, 197739, 197737, 197735, 197733, 197732, 197731, 197730, 197728, 197727, 197726, 197725, 197723, 197718, 197716, 197715, 197708, 197705, 197704, 197703, 197697, 197691, 197678, 197676, 195842, 195838, 192725, 190947, 197752, 197722, 197721, 197707, 197702, 197701, 197700, 197693, 197677, 194817, 193638, 192825, 192412, 189332, 200611, 200608, 200607, 199533, 199424, 199318, 199186, 198636, 198520, 198519, 198400, 197983, 197957, 197781, 197779, 197778, 197777, 197771, 197758, 197755, 197749, 197748, 197729, 197724, 197717, 197706, 197686, 197659, 197080, 194810, 194753, 194513, 193794, 193644, 193642, 188215, 182616, 161141, 197719, 197714, 181239, 197776, 197651, 193648, 203274, 203138, 203109, 203048, 203021, 202910, 202804, 202232, 203258, 203243, 203165, 203053, 202996, 203125, 203106, 203089, 203088, 203087, 203086, 203052, 203049, 203048, 203044, 203035, 203033, 203021, 203016, 202994, 202993, 202987, 202895, 202651, 202652, 202232, 202206, 201593, 203035
        ];
        $arr2 = [
            68787, 92993, 196256, 161101, 145307, 114402, 98909, 27439, 55531, 164150, 196755, 139269, 95814, 69459, 86274, 113450, 125305, 109772, 123439, 28685, 73542, 107068, 195940, 145197, 133519, 119777, 158170, 135018, 138197, 142823, 195051, 99713, 181816, 110489, 138481, 27021, 159776, 161672, 3810, 101031, 143108, 132625, 78528, 182370, 75318, 151073, 110570, 102960, 123729, 48676, 120477, 52498, 187083, 149870, 96247, 97270, 142495, 101055, 69338, 53971, 107236, 54144, 200694, 100751, 190921, 47935, 1931, 128555, 152253, 50709, 115029, 190535, 135312, 171752, 119943, 162363, 181381, 182369, 172211, 55505, 12409, 10606, 105890, 115560, 160831, 127938, 123326, 186981, 55538, 183542, 123742, 181033, 52597, 78735, 186633, 130041, 128158, 116705, 118456, 50800, 14669, 53937, 104445, 143435, 109846, 124528, 21188, 110202, 142683, 82113, 78339, 196468, 72314, 75767, 130045, 110089, 138118, 95873, 55768, 71772, 134385, 11315, 114204, 144084, 111473, 97341, 94379, 3010, 182104, 5194, 143902, 109986, 139760, 114103, 157178, 77037, 62371, 172205, 6306, 100561, 28916, 27395, 155451, 161069, 197613, 94222, 200774, 126308, 93453, 101824, 125657, 144725, 113749, 181804, 186191, 198621, 71772, 96324, 181032, 43532, 98814, 92370, 116720, 138442, 98730, 55534, 185724, 26161, 150632, 121467, 26329, 125421, 21182, 117238, 23775, 61547, 28327, 196597, 62411, 110845, 93452, 69, 119729, 41919, 12048, 34789, 172961, 160411, 48707, 151145, 7087, 139986, 199625, 158246, 53421, 115211, 55771, 107773, 139263, 101702, 160957, 133758, 120870, 78398, 99106, 106289, 54242, 54136, 133147, 34571, 99095, 164236, 44801, 187264, 3696, 127569, 81564, 125144, 92937, 160106, 4264, 163592, 162979, 196387, 172717, 172041, 160980, 187097, 87343, 158005, 69814, 187936, 81210, 90044, 24462, 117966, 28114, 164262, 76263, 112809, 157179, 127557, 184501, 194675, 182268, 157442, 117809, 111585, 104281, 184665, 158242, 197261, 92992, 110504, 163782, 123821, 71348, 19811, 54034, 142744, 160008, 1109, 5169, 115974, 27797, 4310, 159774, 52155, 184332, 68465, 100172, 90499, 121490, 185209, 96039, 138540, 162808, 142743, 79499, 118919, 102333, 43701, 109959, 59407, 33065, 89337, 164245, 96276, 96269, 96318, 126484, 200295, 128665, 4828, 144555, 94248, 92067, 11259, 140097, 173807, 119496, 63078, 92381, 150833, 33941, 54142, 151786, 54038, 115131, 101953, 109771, 135616, 71659, 145346, 116164, 34501, 28779, 194177, 19160, 164363, 196557, 160147, 52156, 25947, 150470, 157925, 140040, 116355, 134900, 99612, 26236, 118898, 357, 40235, 161298, 73331, 101107, 94377, 1759, 184496, 160033, 178493, 163255, 184712, 7363, 157923, 41321, 115585, 100561, 151558, 134584, 129765, 133363, 7760, 58750, 150834, 93598, 114010, 54143, 200871, 200696, 33514, 185650, 75392, 52911, 97792, 76243, 76656, 41325, 198819, 83328, 111992, 196459, 35724, 181831, 95030, 109821, 97605, 159992, 94378, 193471, 119944, 99664, 150903, 186647, 173807, 949, 71379, 45642, 180711, 182536, 142495, 48162, 109734, 116960, 115664, 114588, 187427, 61974, 138164, 182637, 164261, 197316, 128660, 95762, 143528, 16846, 109898, 138574, 161360, 134420, 98440, 94221, 160138, 160717, 200730, 55536, 111136, 24451, 192785, 109803, 40467, 160914, 139818, 138825, 56412, 95864, 191945, 43509, 92377, 161105, 142311, 72953, 121951, 163504, 45249, 162607, 110313, 121784, 162738, 196627, 109821, 183262, 160329, 101708, 56867, 51427, 136244, 104390, 58891, 50289, 76819, 157576, 71920, 73013, 150837, 144511, 41372, 100556, 96684, 97270, 47750, 106011, 1814, 182840, 150636, 5190, 115543, 61874, 81182, 181184, 5518, 183604, 139843, 140886, 164026, 108113, 119408, 114123, 145484, 20325, 83755, 95111, 117442, 172892, 195143, 51675, 7467, 71222, 94376, 71072, 160903, 36693, 157676, 171881, 117208, 160863, 67144, 102990, 143974, 109791, 128658, 107252, 144212, 69801, 142754, 22635, 55772, 18180, 95874, 2828, 143309, 157495, 189082, 70605, 95868, 83920, 97794, 150969, 164406, 101898, 54243, 91891, 93898, 197214, 189912, 38892, 199310, 160312, 96710, 125475, 28362, 21239, 183472, 110202, 55532, 190173, 195561, 151976, 23891, 172150, 161217, 92939, 29836, 182349, 86451, 180640, 108827, 109915, 125656, 4755, 110903, 111385, 10608, 125107, 160741, 25986, 187886, 109755, 184719, 69177, 75648, 196704, 23321, 107107, 110547, 79196, 95416, 121783, 105680, 92938, 112284, 143775, 58653, 121515, 7723, 79686, 137282, 196044, 96330, 87973, 82241, 128461, 71381, 195543, 133895, 195595, 135893, 97768, 122840, 191759, 57766, 65703, 118760, 54039, 88818, 110177, 81203, 7494, 108704, 38153, 199181, 126927, 132053, 77945, 117718, 141608, 101156, 172953, 126484, 186726, 122404, 111003, 66837, 141188, 29004, 119217, 117266, 111472, 192425, 185319
        ];
        $arr3 = [
            13106, 65889, 104367, 81012, 150459, 70806, 2492, 96790, 46186, 34165, 29487, 140832, 151518, 40282, 1573, 47754, 184590, 107393, 30629, 111819, 140393, 78818, 40042, 49824, 57261, 6038, 140551, 50478, 39315, 57948, 4903, 24484, 35832, 28136, 114853, 141850, 131978, 3814, 34541, 82989, 24483, 95407, 94434, 139168, 43506, 66978, 185866, 197370, 12422, 79376, 81822, 197172, 40480, 47106, 119213, 30699, 40270, 69050, 46658, 143857, 31894, 150985, 88373, 109690, 109580, 65728, 133045, 34688, 92857, 136178, 83900, 185579, 21474, 110836, 11328, 33652, 151803, 31403, 75072, 25522, 29335, 29219, 63578, 24065, 77191, 104433, 25506, 24147, 14055, 150970, 125311, 30382, 172772, 101430, 92726, 81587, 43932, 180670, 17633, 181206, 30237, 279, 30208, 1089, 33077, 196778, 26958, 68782, 37390, 19685, 13974, 75499, 131996, 143992, 21929, 139688, 58522, 93693, 120191, 157802, 23499, 16041, 13354, 122150, 35243, 36170, 37479, 133359, 88017, 105283, 109581, 92894, 23269, 14050, 19411, 116062, 22144, 142340, 50605, 10045, 36683, 134664, 187604, 182804, 52235, 99588, 181014, 88409, 104715, 140415, 71368, 160971, 12944, 140616, 103154, 25968, 13754, 185593, 26840, 108789, 31903, 104898, 184429, 79859, 8968, 160952, 25965, 16126, 85702, 65695, 49102, 37960, 160721, 132422, 67647, 94708, 103095, 70795, 117551, 64903, 14127, 110041, 81498, 94530, 190883, 11675, 87831, 53073, 118826, 157588, 25970, 117190, 124276, 77927, 10523, 88733, 183592, 25966, 38531, 12458, 48684, 118336, 24565, 31922, 16009, 139184, 18053, 6975, 54271, 33653, 93679, 37111, 16812, 32271, 16021, 3706, 110835, 141215, 102821, 121603, 5857, 14041, 37686, 182160, 187603, 93139, 26530, 32563, 28052, 159162, 49274, 129956, 16862, 109362, 35761, 137894, 121101, 141103, 27069, 49193, 88526, 159137, 11951, 6981, 36069, 37638, 57262, 30529, 44343, 49085, 19384, 195968, 84997, 16260, 28821, 95344, 33743, 5028, 102820, 141861, 14767, 40847, 12461, 103982, 40821, 76482, 89188, 86020, 43749, 32959, 67421, 131585, 686, 56871, 44835, 151463, 131759, 35287, 55824, 182459, 43552, 108333, 185834, 16273, 33590, 11949, 5220, 83524, 39752, 8122, 57720, 27245, 119250, 128558, 26209, 133849, 63528, 187168, 39927, 137892, 92340, 159355, 117296, 184549, 110817, 145245, 181603, 5836, 18108, 53623, 67054, 37160, 28053, 27215, 8121, 5590, 6298, 37934, 15049, 77022, 10223, 128096, 110886, 8971, 198658, 65964, 23465, 66629, 31238, 118088, 48864, 133048, 193297, 34902, 79781, 119017, 55823, 4958, 137540, 14785, 36046, 139860, 42846, 104551, 104464, 40152, 126441, 135489, 39861, 111214, 160827, 89183, 149724, 57121, 83356, 57171, 14071, 143993, 140416, 47245, 12157, 16028, 66554, 12840, 6362, 68553, 92858, 125778, 151958, 133245, 63783, 95991, 80517, 137496, 160865, 20732, 80031, 139276, 194378, 90069, 67375, 98938, 14988, 99392, 191139, 27403, 14311, 109758, 49852, 98801, 114857, 20969, 12557, 109691, 50034, 11115, 18113, 1484, 57620, 110463, 65908, 5395, 59739, 66528, 95536, 66002, 150619, 58567, 47863, 108654, 109860, 94508, 65855, 196934, 97848, 134332, 79613, 23220, 77539, 104230, 106283, 180997, 92440, 54527, 42664, 21396, 16282, 79785, 55208, 21485, 106476, 12240, 33812, 29415, 22238, 193699, 19221, 39726, 139274, 32296, 99836, 13005, 183071, 21860, 108754, 13709, 31569, 119251, 182816, 119242, 103410, 98632, 29077, 104138, 133046, 4703, 185188, 31286, 137535, 120936, 31404, 143895, 104373, 152109, 77538, 152077, 73382, 79614, 142341, 135448, 31280, 71153, 137895, 33332, 119899, 69322, 14178, 30557, 23081, 118663, 31881, 143971, 38577, 103084, 109046, 150462, 30386, 133973, 96436, 5635, 105107, 43526, 37992, 110833, 75017, 135924, 139275, 39995, 50997, 127879, 56733, 67347, 132647, 105106, 106284, 14023, 20479, 22064, 70882, 30637, 187170, 93264, 43568, 109036, 29067, 29469, 35763, 12509, 45589, 77063, 99720, 139897, 112919, 49919, 68512, 138295, 54270, 26138, 139899, 69043, 12585, 77841, 164816, 151591, 29224, 104714, 6014, 159353, 14787, 64921, 86684, 78862, 53049, 79706, 115201, 39589, 22395, 98766, 15046, 6015, 95108, 75558, 117299, 26001, 14604, 40160, 27078, 118086, 16113, 74252, 34378, 196899, 99242, 145477, 45414, 54952, 20490, 17381, 12188, 13182, 43569, 18352, 84431, 20169, 84120, 25307, 52254, 135211, 11370
        ];
        $wikiIds = $arr1;
        switch ($classifyId) {
            case 220118:
                $wikiIds = $arr1;
                break;
            case 220119:
                $wikiIds = $arr2;
                break;
            case 220120:
                $wikiIds = $arr3;
                break;
        }
        //偏移量，当前页-1乘以每页显示条数
        $start = ($page - 1) * $perPage;

        shuffle($wikiIds);
        return array_slice($wikiIds, $start, $perPage);
    }
}
